<?php
	//fil d'ariane
	$ariane = array(
		array(
			'href' => "index.php",
			'name' => "Home"
		),
		array(
			'href' => "profile.php",
			'name' => "Profile"
		)
	);
	
	require("include/includes.php");
	
	/*
		init de twig déplacé dans './include/lib/startTwig.lib.php' appelé dans includes.php
	*/
	
	$player = NULL;
	$url=NULL;
	$records = NULL;
	$isOwnerProfil = false;
	
	if(isset($_GET["pid"]) && is_numeric($_GET["pid"]))
	{
		$profilId = $_GET["pid"];
	}
	else
	{
		$profilId = 0;
	}
	
	if(isset($_SESSION["user"]))
	{
		if($profilId == $_SESSION["user"]->getId() || $profilId == 0)
		{
			$isOwnerProfil = true;
			$profilId = $_SESSION["user"]->getId();
		}
	}
	$player = new Player($profilId);
	if($player->getNickname())
	{
		$url = "http://".$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		
	
		/************ DEBUT FILTRAGE ************/
		$begin 		= getOrCustom('begin', 0);
		$nb 		= 100;
		$country	= getOrCustom('country', -1);
		$penta		= (getOrCustom('penta', 0) == 'on')? 1:0;
		$pentaSelected = ($penta==1)? 'on':'off';
		$stp		= getOrCustom('stp', 0);
		$ice		= getOrCustom('ice', 0);

		$type 		= getOrCustom('type', COURSE);
		$trackType 	= new TrackType($type);
		$typeName 	= $trackType->getName();
	
		$mode 		= getOrCustom('mode', CLASSIC);
		$trackMode 	= new TrackMode($mode);
		$modeName 	= $trackMode->getMode();
		
		// Si $type = n'importe quoi et que ça ne corresponde à rien
		if(empty($typeName)){
			$trackType = new TrackType($type);
			$typeName = $trackType->getName();
		}
		
		if(empty($modeName)){
			$trackMode = new TrackMode($mode);
			$modeName = $trackMode->getMode();
		}
		
		if($modeName=="classic")
		{
			$ice = 0;
			$stp = 0;
		}
		
		//$url = "http://".$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		$records = Record::getAllWithFilter(Array(
			'country'=>$country, 
			'penta'=>$penta, 
			'stp'=>$stp, 
			'ice'=>$ice,
			'player'=>$profilId, 
			'type'=>$type,
			'mode'=>$mode,
			'begin'=>$begin, 
			'nb'=>$nb
		));
		
		/************ FIN FILTRAGE ************/
	
		$types = TrackType::getAll();
		$modes = TrackMode::getAll();
		$modeSelected = 0;
		$typeSelected = 0;
		// Création du titre
		$pageTitle = 'Ranking: ';
		if(isset($_GET["mode"]) && isset($_GET["type"]) && $_GET["mode"]!=0) {
			$pageTitle .= $typeName." - ".$modeName;
			$modeSelected = $_GET["mode"];
			$typeSelected = $_GET["type"];
		}
		else if(isset($_GET["mode"]) && !isset($_GET["type"])) {
			$pageTitle .= $modeName;
			$modeSelected = $_GET["mode"];
			$typeSelected = 0;
		}
		else if((!isset($_GET["mode"]) || $_GET["mode"] == 0) && isset($_GET["type"])) {
			$pageTitle .= $typeName;
			$modeSelected = 0;
			$typeSelected = $_GET["type"];
		}
		
		if(!isset($_SESSION["user"]))
			$userId = 0;
		else
			$userId = $_SESSION["user"]->getId();
		
		$template = $twig->loadTemplate('profile.html');
		$template->display(array(
			'pageTitle' => 'Profile',
			'player' => $player,
			'url' => $url,
			'records' => $records,
			'isOwnerProfil' => $isOwnerProfil,
			'profilId' => $profilId,
			'types' => $types,
			'modes' => $modes,
			'modeSelected' => $modeSelected,
			'typeSelected' => $typeSelected,
			'pentaSelected' => $pentaSelected,
		));
	}
	else // AUCUN UTILISATEUR trouvé dans la bdd avec cet ID
	{
		$template = $twig->loadTemplate('error.html');
		$template->display(array(
			'pageTitle' => 'Profil',
			'message' => 'Oops! I can\'t find this player!'
		));
	}
?>